from django.template import loader
from django.conf import settings
import os

from contents.models import ContentCategory
from contents.utils import get_categories


def generate_static_index_html():
    categories = get_categories()
    contents = {}
    content_categories = ContentCategory.objects.all()
    for content_category in content_categories:
        contents[content_category.key] = content_category.content_set.filter(status=True).order_by('sequence')

    context = {
        'categories': categories,
        'contents': contents
    }

    template = loader.get_template('index.html')
    index_html = template.render(context)

    with open(os.path.join(settings.STATICFILES_DIRS[0], 'index.html'), 'w', encoding='utf-8') as f:
        f.write(index_html)
